unit clsBordero;

interface

uses Dm, dbtables, biblio;

type
  TBordero = class(TObject)
  private
    Bord_Numero: string;
  public
    Bord_Mensagem: string;
    Bord_DataEmis: TDateTime;
    Bord_PagRec: string[1];
    function Gravar(p_Bord_Numero: string): Byte;
    function Recuperar(p_Bord_Numero: string): Byte;
    function Excluir(p_Bord_Numero: string): Byte;
    function Existe_Titulo(p_Bord_Numero, p_Tipd_Codigo, p_Titc_Numero, p_Ent_Codigo: string): Boolean;
    procedure Consultar(var p_q: TQuery);
  end;

implementation

function TBordero.Gravar;

var
  tq: TQuery;

begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Bordero where Bord_Numero=:k_Bord_Numero');
  tq.parambyname('k_Bord_Numero').asstring := p_Bord_Numero;
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO Bordero VALUES(:k_Bord_Numero,:k_Bord_Mensagem,:k_Bord_DataEmis,:k_Bord_PagRec)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE Bordero SET Bord_Mensagem=:k_Bord_Mensagem,Bord_DataEmis=:k_Bord_DataEmis, Bord_PagRec =:k_Bord_PagRec WhERE ' +
      'Bord_Numero=:k_Bord_Numero');
  end;
  tq.parambyname('k_Bord_Numero').asstring := p_Bord_Numero;
  tq.parambyname('k_Bord_Mensagem').asstring := Bord_Mensagem;
  tq.parambyname('k_Bord_DataEmis').asdatetime := Bord_DataEmis;
  tq.ParamByName('k_Bord_PagRec').AsString := Bord_PagRec;
  tq.close;
  try
    tq.execsql;
    result := 1;
  except
    result := 0;
  end;
  tq.Free;
end;

function TBordero.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Bordero where Bord_Numero=:k_Bord_Numero');
  tq.Params[0].asstring := p_Bord_Numero;
  tq.open;
  Bord_Numero := p_Bord_Numero;
  if tq.eof then Result := 0 else Result := 1;
  Bord_Mensagem := tq.fieldbyname('Bord_Mensagem').asstring;
  Bord_DataEmis := tq.fieldbyname('Bord_DataEmis').asdatetime;
  Bord_PagRec := tq.FieldByName('Bord_PagRec').AsString;
  tq.Close;
  tq.Free;
end;

function TBordero.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM Bordero WHERE Bord_Numero=:k_Bord_Numero');
  tq.parambyname('k_Bord_Numero').asstring := p_Bord_Numero;
  try
    tq.execSQL;
    Result := 1;
  except
    result := 0;
  end;
  tq.close;
  tq.Free;
end;

procedure TBordero.Consultar;
begin
  p_q.close;
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from Bordero order by bord_numero desc');
  p_q.open;
end;

function TBordero.Existe_Titulo;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := false;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from TitCredito where (Bord_Numero<>:a or Bord_Numero=:b) and Tipd_Codigo=:c and ' +
    'Titc_Numero=:d ');
  if p_Ent_Codigo <> '' then
    tq.sql.add('and Ent_Codigo=:e');
  tq.params[0].asstring := p_Bord_Numero;
  tq.params[1].asstring := p_Bord_Numero;
  tq.params[2].asstring := p_Tipd_Codigo;
  tq.params[3].asstring := p_Titc_Numero;
  if p_Ent_Codigo <> '' then
    tq.params[4].asstring := p_Ent_Codigo;
  tq.open;
  if tq.eof then result := false else result := true;
  tq.close;
  tq.Free;
end;

end.
